$(document).ready(function() { // ready(): event method
$('p').click(function() { // click(): event method
$(this).css('background-color', 'Red'); // css(): action method
});
});
$.get(url, function(data) { $('#message-pane').html(data); }); // get(): AJAX method; html(): action method
$ for your library? Can you use another name, such as TRUjQ, instead of $?$() in the above example?documentthis as a HTMLElement object$ in the above example? Object? Function?$ in the above example?$ in the above example?.get()TRUjQ(selector).actionoreventmethod(); TRUjQ.ajaxmethod(); TRUjQ.property;documentthis as a DOM object, i.e., HTMLElement objectTRUjQ_document - from TRUjQ(document)TRUjQ_htmlselector - from TRUjQ('...')TRUjQ_htmlelement - from TRUjQ(this) or TRUjQ(HTMLElement)ready() for document (actually the 'load' event on window)click()dblclick()html()attr()css()hide(), show(), toggle()get()post()version - E.g., TRUjQ.versionTRUjQ()typeof argument == 'string'argument instanceof HTMLElementargument == document
function TRUjQ(x) {
if (x == document) {
...
return ...;
} else if (typeof x == 'string') {
...
return ...;
} else if (x instanceof HTMLElement) {
...
return ...;
} else // unknown case
return null;
}
TRUjQ.version.
function TRUjQ(x) {
if (x == document) { ... }
else if (typeof x == 'string') { ... }
else if (x instanceof HTMLElement) { ... }
else { ... }
}
TRUjQ.version = '0.0.1';
document.getElementsByClassName()
document.getElementsByTagName()
document.querySelectorAll()
this is the selected HTMLElement when event listeners are invoked.
this.
The this in TRUjQ_test_id('#...').click(function() { ...this... }) is not the element of the given id.
Why?
this in TRUjQ_test_id('#...').click(function() { ...this... }) represent the element of the given id?
this for callback functions.
window.getComputedStyle(HTMLElement).property
TRUjQ() and three TRUjQ objectsTRUjQ_document - from TRUjQ(document)TRUjQ_htmlselector - from TRUjQ('...')TRUjQ_htmlelement - from TRUjQ(this)$(document).ready(...), $('...').click(...) and $(this).dblclick(...)TRUjQ_document - How to make a TRUjQ object similar to the return value from jQuery $(document)?TRUjQ(document).ready(function(){...});TRUjQ(document) return?.ready(), that has an argument of a callback function
that should be invoked when the document is completely loaded.
This object also has some other methods to support events such as 'click' and 'dblclick'.
<script>
//-- library -------------------------------
function TRUjQ(x) {
if ????
return ??? _TRUjQ_document(???); // x; this should NOT be passed; this is window.
}
function _TRUjQ_document(thispreserved) { // Object constructor
this.ready = function(callback) {
if (document.readyState == 'complete') {
// callback(); // Should not be called directly. If so, the value of this in callback becomes different.
// Let's use the next interesting trick.
thispreserved.__TRUjQ_callback = callback;
????
// or instead of the above trick, callback.bind(thispreserved)()
// See https://www.w3schools.com/js/js_function_bind.asp
// or
// callback.call(thispreserved); // https://www.w3schools.com/js/js_function_call.asp
} else {
???.???(???, function() {; // onload event on window; thispreserved should be used in the callback function.
????
});
}
}
this.click = function(callback) {
document.addEventListener('click', function(eobj) {
//callback(); // It will not preserve 'this'. How to fix?
????
});
}
// ????
}
</script>
<script>
//-- application ---------------------------
TRUjQ(document).ready(function() {
document.getElementById('tr2-div').innerHTML = 'Ready! Click me!<br>';
});
TRUjQ(document).click(function() {
document.getElementById('tr2-div').innerHTML += (this == document ? 'this is document.' : 'this is not document.') + '<br>';
document.getElementById('tr2-div').innerHTML += 'Clicked<br>';
});
</script>
<div id='tr2-div'>Message here!</div>
TRUjQ_htmlselector - How to make a TRUjQ object similar to the return value from $('...')?TRUjQ('...').click(function(){...});, TRUjQ('...').dblclick(function(){...});TRUjQ('...') return?.click() and .dblclick(), that have an argument of a callback function
that should be invoked when there is a 'click' or 'dblclick' event triggered on the target HTMLElement object.
This object also has some action methods.
<script>
//-- library -------------------------------
function TRUjQ(x) {
if (x == document) {
return new _TRUjQ_document(x);
}
else if ????
return ??? _TRUjQ_htmlselector(x);
}
function _TRUjQ_document(thispreserved) { // Object constructor
this.ready = function(callback) {
if (document.readyState == 'complete') {
thispreserved.__TRUjQ_callback = callback;
thispreserved.__TRUjQ_callback();
} else
window.addEventListener('load', function() { // onload event on window
(callback.bind(thispreserved))(); // or, instead of the trick in the above
});
}
// ????
}
function _TRUjQ_htmlselector(selector) {
selector = selector.trim();
var elements = ????
this.??? = function(callback) {
???? // Registration of event listener over selected elements; how?
// Need to be careful with callback and this. What should be bound to callbak?
}
// ????
}
</script>
<script>
//-- application ---------------------------
TRUjQ(document).ready(function() {
TRUjQ('p').click(function() {
alert('Wow! Interesting! Tag selection, ' + this.innerHTML);
});
TRUjQ('#tr3-p').click(function() {
alert('Wow! Interesting! Id selection, ' + this.innerHTML);
});
TRUjQ('.tr3-p').click(function() {
alert('Wow! Interesting! Class selection, ' + this.innerHTML);
});
});
</script>
<p style='border:solid 1px Red'>You look great today.</p>
<p id='tr3-p' style='border:solid 1px Blue'>You are a fantastic student.</p>
<p class='tr3-p' style='border:solid 1px Green'>You have the best style.</p>
TRUjQ_htmlelement - How to make a TRUjQ object similar to the return value from $(this)?TRUjQ(this).css(); TRUjQ(this).hide(); ...TRUjQ(this) return?
<script>
//-- library -------------------------------
function TRUjQ(x) {
if (typeof x == 'string')
return new _TRUjQ_htmlselector(x);
else if (????)
return new _TRUjQ_htmlelement(x); // If HTMLElement object
}
function _TRUjQ_htmlselector(selector) {
selector = selector.trim();
var elements = document.querySelectorAll(selector);
this.click = function(callback) {
for (var i = 0; i < elements.length; i++) {
elements[i].addEventListener('click', function(obj) {
this.__TRUjQ_callback = callback;
this.__TRUjQ_callback(obj);
});
}
}
this.hide = ????
// ????
}
function _TRUjQ_htmlelement(element) { // element: HTMLElement object
// ????
this.css = function(property, value) {
if (value == ???) // when value is not defined,
return ????; // Using window.getComputedStyle()
else
????; // Set property without using window.getComputedStyle()
};
// ????
}
</script>
<script>
//-- application ---------------------------
TRUjQ('p').click(function() {
TRUjQ(this).css('background-color', 'SkyBlue');
});
TRUjQ('#tr4-button').click(function() {
TRUjQ('#tr4-p').hide();
});
</script>
<p id='tr4-p' style='border:solid 1px Blue'>Click it to change the background color. You can do it!</p>
<button id='tr4-button'>Hide it!</button>
TRUjQ.get()TRUjQ.get(url, callback);
<script>
//-- library -------------------------------
TRUjQ = ???; // Let's start it with an empty object for testing.
TRUjQ.??? = function(????) {
let xhttp = ??? ???(); // AJAX object
xhttp.addEventListener('load', function() {
????
});
xhttp.???("GET", url);
xhttp.???();
}
</script>
<script>
//-- application ---------------------------
//- Let's use jQuery for testing, except TRUjQ.get(). -----------------
$('#tr5-send').click(function() {
let url = `echo.php?message=????`; // message from <input>
TRUjQ.get(????); // the message sent from the server needs to be displayed in <div>
});
</script>
<input id='tr5-message'><br>
<button id='tr5-send'>Send the above message to the echo server!</button>
<div id='tr5-result'>Echoed message here!</div>
TRUjQ.get().
Here is an example how to use this version.
const testTRUjQGet = async function(url) {
let data = await TRUjQ.get(url);
...
}
testTRUjQGet("echo.php?message='I am happy to see your smile.'");
Can you implement this version of TRUjq.get() so that
the server-side can be accessed like a database?
TRUjQ.post() and TRUjQ(selector).load()TRUjQ.post(url, queryobject, callback);, or
... = await TRUjQ.post(url, queryobject);
TRUjQ(selector).load(url);